package com.intellagric.service.iftmonitor.Impl.ift_equipment;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.iftmonitor.EquipmentDao;
import com.intellagric.mapper.IftEquipmentMapper;
import com.intellagric.module.iftmonitor.EquipmentVo;
import com.intellagric.pojo.IftEquipment;
import com.intellagric.pojo.IftEquipmentExample;
import com.intellagric.service.iftmonitor.ift_equipment.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/20
 * @DESC： 普通设备管理服务实现
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private IftEquipmentMapper equipmentMapper;

    @Autowired
    private EquipmentDao equipmentDao;

    /**
     * 添加设备
     * @param equipment
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addEquipment(IftEquipment equipment) {
        if (equipmentMapper.insertSelective(equipment) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除设备记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteEquipment(String id) {
        if (equipmentMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改设备信息
     * @param equipment
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editEquipment(IftEquipment equipment) {
        if (equipmentMapper.updateByPrimaryKeySelective(equipment) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询设备详细信息
     * @param id
     * @return IftEquipment
     */
    @Override
    public IftEquipment getEquipmentById(String id) {
        return equipmentMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询设备信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentList() {
        IftEquipmentExample equipmentExample = new IftEquipmentExample();
        equipmentExample.createCriteria().andIdIsNotNull();
        List<IftEquipment> equipmentList = equipmentMapper.selectByExample(equipmentExample);
        return LayuiDataGridResult.success().add(equipmentList,equipmentList.size());
    }


    /**
     * 多条件查询设备信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentListByVo(EquipmentVo equipmentVo) {
        List<IftEquipment> equipmentList = equipmentDao.selectEquipmentListByVo(equipmentVo);
        return LayuiDataGridResult.success().add(equipmentList,equipmentList.size());
    }

    /**
     * 分页查询设备列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        IftEquipmentExample equipmentExample = new IftEquipmentExample();
        equipmentExample.createCriteria().andIdIsNotNull();
        List<IftEquipment> equipmentList = equipmentMapper.selectByExample(equipmentExample);
        return LayuiDataGridResult.success().add(equipmentList,this.getEquipmentList().getCount());
    }
}
